import axios, { InternalAxiosRequestConfig } from 'axios';
import { IHttpResponse } from '../../net/http-response';

/**
 * UniApp 请求响应对象
 *
 * @export
 * @class UniResponse
 * @implements {IHttpResponse<T>}
 * @template T
 */
export class UniResponse<T = IData> implements IHttpResponse<T> {
  /**
   * 是否为本地
   *
   * @memberof UniResponse
   */
  local = true;

  /**
   * 请求状态
   *
   * @memberof UniResponse
   */
  ok = false;

  /**
   * 请求数据
   *
   * @type {T}
   * @memberof UniResponse
   */
  data: T;

  /**
   * 状态码
   *
   * @type {number}
   * @memberof UniResponse
   */
  status: number;

  /**
   * 状态文本描述
   *
   * @type {string}
   * @memberof UniResponse
   */
  statusText: string;

  /**
   * 响应头
   *
   * @type {IData}
   * @memberof UniResponse
   */
  headers: IData;

  config: InternalAxiosRequestConfig<IData> = {
    headers: new axios.AxiosHeaders(),
  };

  /**
   * Creates an instance of UniResponse.
   * @param {IData} headers 相应头
   * @param {unknown} [data]
   * @param {number} [status]
   * @param {string} [statusText]
   * @memberof UniResponse
   */
  constructor(response: UniApp.RequestSuccessCallbackResult) {
    this.data = response.data as T;
    this.headers = response.header;
    this.status = response.statusCode || 200;
    this.statusText = '';
    if (this.status >= 200 && this.status < 300) {
      this.ok = true;
    }
  }
}
